package com.fangmou.ing.publish.freemarker;

import java.io.IOException;
import java.io.Writer;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class DirectiveBodyWraper implements TemplateDirectiveBody,TemplateModel{
  private TemplateDirectiveBody body;
  public DirectiveBodyWraper parentBody;
  public Environment env;
  
  public DirectiveBodyWraper(TemplateDirectiveBody body,
          Environment env) {
      super();
      this.body = body;
      this.env = env;
  }
  
  public void render(Writer out) throws TemplateException, IOException {
      
      DirectiveBodyWraper preOverridy = (DirectiveBodyWraper)env.getVariable(DirectiveUtils.OVERRIDE_CURRENT_NODE);
      try {
          env.setVariable(DirectiveUtils.OVERRIDE_CURRENT_NODE, this);

          if(body != null){
            body.render(out);
          }
      }finally {
          env.setVariable(DirectiveUtils.OVERRIDE_CURRENT_NODE, preOverridy);
      }
  }
}
